<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      width="30%"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <FormPanel
        ref="FormPanel"
        :cols="1"
        :list="topFilterList2"
        :isSubmitBtn="false"
      />
      <div style="text-align: center;">
        <el-button size="small" type="primary" @click="cancle">取消</el-button>
        <el-button size="small" type="primary" @click="saveHandle">保存</el-button>
      </div>
    </BaseDialog>

  </div>
</template>

<script>
import {
  qrySortDefinitionDetail,
  saveOrUpdateSortDefinition,
  qrySortDefinitionPage
 } from '@/api/common/cars/vehicleClassification';
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { IDNumber, phone,endTime, email, formatDates, checkPassport} from '@/utils/validate'
export default {
  name: 'vehicleClassification',
  mixins: [dictionary,authority],
  data() {
    return {
      topFilterList: this.createTopFilters(),
      topFilterList2: this.createTopFilters2(),
      fetchParams: {
      },
      visible: false,
      dialogTitle: '',
      fetchApi: qrySortDefinitionPage,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf:{ add: '新增', edit: '编辑', show: '显示' },
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '分类代码',
          fieldName: 'basedataType',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '分类名称',
          fieldName: 'basedataName',
          placeholder: '请输入'
        }
      ];
    },
   createTopFilters2() {
      return [
        {
          type: 'INPUT',
          label: '分类代码',
          fieldName: 'basedataType',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '分类名称',
          fieldName: 'basedataName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
       {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '请选择',
          initialValue:10451001,
          itemList: this.createDictList('1045'),
          disabled:true,
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: (props, h) => {
            /*
            <multiuse-button size="mini" divider="before" type="text" onClick={() => this.editHandle(props.row)} auth-list={this.auths} auth-mark={'/api/basedata/vehicleProduct/car/editCarPrice'}>
                  编辑
                </multiuse-button> */
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>

              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '分类代码',
          dataIndex: 'basedataType',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分类名称',
          dataIndex: 'basedataName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          width: 120,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        }
      ];
    },
    //新增
    addHandle(){
      this.handleType = 'add';
      this.dialogTitle = `新增整车分类`;
      this.visible = true;
      this.topFilterList2.map(x=>{
        x.initialValue = []
      })
      this.topFilterList2.find(x=>x.fieldName == 'dataSources').initialValue = 10451001
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.dialogTitle = `编辑整车分类`;
      this.visible = true;
      this.formData = row;
      this.topFilterList2.map(x=>{
        x.initialValue = row[x.fieldName]
      })
    },
    //保存
    saveHandle(){
      let data = this.$refs.FormPanel.SUBMIT_FORM()
      if(this.handleType == 'edit'){
        data.basedataId = this.formData.basedataId
      }
      if(data){
        console.log(data,"保存数据")
        saveOrUpdateSortDefinition(data).then(res=>{
          if(res.resultCode == 200){
            this.$message({
              type: 'success',
              message: '保存成功!'
            });
            // 重新加载
            this.fetchParams = { ...this.fetchParams };
            this.cancle('refresh')

          }
        })
      }
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({},val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    cancle(val, type) {
      this.visible = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
  },
  created() {
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
